// pages/shop/index/index.js
import api from '../../../api/api.js'
import load from '../../templates/loader/loader.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrayData: [],
    collectionState: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //初始化载入等待动画
    that.loader = load.loader(that, {
      data: "loader",
      toast: true,
      size: 50,
      borderWidth: 3
    });
    that.loader.open(); //打开载入等待
    api.getUser({
      done: (user) => {
        that.data.userInfo = user
        that.loadData(options)
      }, fail: ()=>{
        wx.redirectTo({
          url: '/pages/login/login',
        });
      }
    })
  },
  loadData: function (options) {
    var that = this
    var jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"userId":"' + that.data.userInfo.id + '","vendor_id":"' + options.vendor_id + '"}}';
    api.getShop({
      method: 'POST',
      data: {
        REQ_MESSAGE: jsonStr
      },
      success: (res) => {
        // success
        that.loader.close()
        var data = res.rows
        if (JSON.stringify(data) == "[]" || !data) {
          that.loader.open("没有更多数据了");
        } else if (data.length > 0) {
          that.setData({
            arrayData: data,
            collectionState: data[0].isCollected
          })
        } else {
          that.loader.open("没有更多数据了");
        }
      },
      fail: () => {
        that.loader.open(api.statusFail.title);
      }
    })
  },
  setCollection: function () {
    var that = this
    wx.showLoading({
      mask:true
    })
    var jsonStr = ''
    if (!that.data.collectionState) {
      jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"userId":"' + that.data.userInfo.id + '","vendor_id":"' + that.data.arrayData[0].vendorDetails[0].id + '"}}'
      api.setCollection({
        method: 'POST',
        data: {
          REQ_MESSAGE: jsonStr
        },
        success: (res) => {
          wx.hideLoading()
          if (res.REP_BODY.RSPCOD == "000000") {
            wx.removeStorage({
              key: 'collectionMsg'
            })
            that.setData({
              arrayData: that.data.arrayData,
              collectionState: true
            })
            wx.showToast({
              title: '收藏成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '收藏失败',
              image: '/image/ico/error-ico.png',
              duration: 2000
            })
          }
        },
        fail: (res) => {
          wx.hideLoading()
          wx.showToast({
            title: '收藏失败',
            image: '/image/ico/error-ico.png',
            duration: 2000
          })
        }
      });
    } else {
      jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"userId":"' + that.data.userInfo.id + '","type":"1","id":"' + that.data.arrayData[0].vendorDetails[0].id + '" }}'
      //取消收藏
      api.delCollection({
        method: 'POST',
        data: {
          REQ_MESSAGE: jsonStr
        },
        success: (res) => {
          that.loader.close(); //关闭载入动画
          wx.hideLoading()
          if (res.REP_BODY.RSPCOD == "000000") {
            that.setData({
              collectionState: false
            })
            //保存删除id
            wx.setStorage({
              key: 'collectionMsg',
              data: [{}, {
                id: that.data.arrayData[0].vendorDetails[0].id
              }]
            })
          } else {
            wx.showToast({
              title: '取消收藏失败',
              image: '/image/ico/error-ico.png',
              duration: 2000
            })
          }
        },
        fail: (res) => {
          wx.hideLoading()
          wx.showToast({
            title: '取消收藏失败',
            image: '/image/ico/error-ico.png',
            duration: 2000
          })
        }
      });
    }
  }
})